package sing

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	Beans "tuowei.com/pkg/sing/beans"
	"tuowei.com/pkg/sing/http_utils"
	"tuowei.com/pkg/sing/sign_service"
	Tools "tuowei.com/pkg/sing/utils"
	"tuowei.com/pkg/xlog"
)

func main1() {

	//配置参数(作为全局执行一次即可)
	sign_service.EsignInitService()
	var initResult []byte
	f, _ := os.Open("demo2.docx")
	info, _ := f.Stat()
	buf := make([]byte, 512)
	f.Read(buf)
	contentType := http.DetectContentType(buf)
	contentMd5, _ := Tools.CountFileMd5("demo2.docx")
	xlog.Debug(contentMd5)
	uploadReq := Beans.UploadFileReq{
		ContentMd5:    contentMd5,
		ContentType:   contentType,
		FileName:      f.Name(),
		FileSize:      info.Size(),
		ConvertToPDF:  false,
		ConvertToHTML: true,
	}
	jsonStrBt, _ := json.Marshal(uploadReq)
	bt, _ := http_utils.SendCommHttp("/v3/files/file-upload-url", string(jsonStrBt), http.MethodPost)
	rsp := Beans.UploadFileRsp{}
	json.Unmarshal(bt, &rsp)
	// 开始真正上传文件
	http_utils.UpLoadFile(rsp.Data.FileUploadUrl, "demo2.docx", contentMd5, contentType)
	//return
	//http_utils.UpLoadFile()
	//return
	//hex.EncodeToString(hash.Sum(nil))
	//创建个人账号-start
	var createPersonInfo Beans.CreatePersonByThirdPartyUserIdInfo
	createPersonInfo.Email = "123@qq.com"
	createPersonInfo.IdNumber = "330************12"
	createPersonInfo.ThirdPartyUserId = "1231ddv1212e1212qre"
	createPersonInfo.Mobile = "18****6670"
	createPersonInfo.Name = "**宇"
	var createPersonInfoJson string
	if data, err := json.Marshal(createPersonInfo); err == nil {
		createPersonInfoJson = string(data)
	}
	initResult = sign_service.CreatePersonByThirdPartyUserId(createPersonInfoJson)
	CreatePsnByThirdData := Tools.BytetoJson(initResult)["data"]
	CreatePsnByThirdAccountId := CreatePsnByThirdData.(map[string]interface{})["accountId"]
	fmt.Println(CreatePsnByThirdAccountId)
	//创建个人账号-end

	//创建企业账号-start
	var createOrganizationsInfo Beans.CreateOrganizationsByThirdPartyUserIdInfo
	createOrganizationsInfo.ThirdPartyUserId = "123fdacq1122ereqerqrew"
	createOrganizationsInfo.Creator = CreatePsnByThirdAccountId.(string)
	createOrganizationsInfo.Name = "***********有限公司"
	createOrganizationsInfo.IdNumber = "982***********C50A"
	createOrganizationsInfo.OrgLegalName = ""
	createOrganizationsInfo.OrgLegalIdNumber = ""
	var createOrganizationsInfoJson string
	if data, err := json.Marshal(createOrganizationsInfo); err == nil {
		createOrganizationsInfoJson = string(data)
	}
	initResult = sign_service.CreateOrganizationsByThirdPartyUserId(createOrganizationsInfoJson)
	CreateOrgByThirdData := Tools.BytetoJson(initResult)["data"]
	CreateOrgByThirdOrgId := CreateOrgByThirdData.(map[string]interface{})["orgId"]
	fmt.Println(CreateOrgByThirdOrgId)
	//创建企业账号-end

	//发起网页版个人认证-start
	var webIndivIdentityUrlInfo Beans.WebIndivIdentityUrlInfo
	webIndivIdentityUrlInfo.AuthType = "PSN_BANK4_AUTHCODE"
	webIndivIdentityUrlInfo.AvailableAuthTypes = []string{"PSN_BANK4_AUTHCODE", "PSN_TELECOM_AUTHCODE", "PSN_FACEAUTH_BYURL"}
	var configParams Beans.ConfigParams
	configParams.IndivUneditableInfo = []string{"name"}
	webIndivIdentityUrlInfo.ConfigParams = configParams
	var webIndivIdentityUrlInfoJson string
	if data, err := json.Marshal(webIndivIdentityUrlInfo); err == nil {
		webIndivIdentityUrlInfoJson = string(data)
	}
	initResult = sign_service.WebIndivIdentityUrl(webIndivIdentityUrlInfoJson, CreatePsnByThirdAccountId.(string))
	WebIndivIdentityUrlData := Tools.BytetoJson(initResult)["data"]
	WebIndivIdentityUrlShortLink := WebIndivIdentityUrlData.(map[string]interface{})["shortLink"]
	fmt.Println(WebIndivIdentityUrlShortLink)
	//发起网页版个人认证-end

	//发起网页版企业认证-start
	var webOrgIdentityUrlInfo Beans.WebOrgIdentityUrlInfo
	webOrgIdentityUrlInfo.AgentAccountId = CreatePsnByThirdAccountId.(string)
	webOrgIdentityUrlInfo.AuthType = "ORG_BANK_TRANSFER"
	webOrgIdentityUrlInfo.AvailableAuthTypes = []string{"ORG_BANK_TRANSFER", "ORG_ZM_AUTHORIZE"}
	var psnConfigParams Beans.ConfigParams
	psnConfigParams.OrgUneditableInfo = []string{"name"}
	webOrgIdentityUrlInfo.ConfigParams = psnConfigParams
	var webOrgIdentityUrlInfoJson string
	if data, err := json.Marshal(webOrgIdentityUrlInfo); err == nil {
		webOrgIdentityUrlInfoJson = string(data)
	}
	initResult = sign_service.WebOrgIdentityUrl(webOrgIdentityUrlInfoJson, CreateOrgByThirdOrgId.(string))
	webOrgIdentityUrlData := Tools.BytetoJson(initResult)["data"]
	webOrgIdentityUrlShortLink := webOrgIdentityUrlData.(map[string]interface{})["shortLink"]
	fmt.Println(webOrgIdentityUrlShortLink)

}
